home *** CD-ROM | disk | FTP | other *** search
/ The World's Largest Collection of Windows Software / The World's Largest Collection of Windows Software - Disc 2.iso / textproc / _j1 / tex2rtf / src / bmputils.h next >
Text File  |  1993-10-17  |  5KB  |  195 lines

  1. /*
  2.  * Utilities for manipulating bitmap and metafile images for
  3.  * the purposes of conversion to RTF
  4.  *
  5.  */
  6.  
  7. char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
  8.   'C', 'D', 'E', 'F' };
  9.  
  10. void DecToHex(int dec, char *buf)
  11. {
  12.   int firstDigit = (int)(dec/16);
  13.   int secondDigit = dec - (firstDigit*16);
  14.   buf[0] = hexArray[firstDigit];
  15.   buf[1] = hexArray[secondDigit];
  16.   buf[2] = 0;
  17. }
  18.  
  19. static unsigned int getshort(FILE *fp)
  20. {
  21.   int c, c1;
  22.   c = getc(fp);  c1 = getc(fp);
  23.   return ((unsigned int) c) + (((unsigned int) c1) << 8);
  24. }
  25.  
  26. static unsigned int getint(FILE *fp)
  27. {
  28.   int c, c1, c2, c3;
  29.   c = getc(fp);  c1 = getc(fp);  c2 = getc(fp);  c3 = getc(fp);
  30.   return ((unsigned int) c) +
  31.          (((unsigned int) c1) << 8) + 
  32.      (((unsigned int) c2) << 16) +
  33.      (((unsigned int) c3) << 24);
  34. }
  35.  
  36. Bool GetBMPHeader(FILE *fp, int *Width, int *Height, int *Planes, int *BitsPerPixel)
  37. {
  38.   int          i, c, c1;
  39.   unsigned int bfSize, bfOffBits, biSize, biWidth, biHeight, biPlanes;
  40.   unsigned int biBitCount, biCompression, biSizeImage, biXPelsPerMeter;
  41.   unsigned int biYPelsPerMeter, biClrUsed, biClrImportant;
  42. //  char         *cmpstr;
  43.   int         *pic24, *pic8;
  44. //  char          buf[512];
  45.  
  46.   /* returns '1' on success */
  47.  
  48.   pic8 = pic24 = (int *) NULL;
  49.  
  50. /*
  51.   fseek(fp, 0L, 2);      // figure out the file size
  52.   filesize = ftell(fp);
  53.   fseek(fp, 0L, 0);
  54. */
  55.  
  56.   /* read the file type (first two bytes) */
  57.   c = getc(fp);  c1 = getc(fp);
  58.   if (c!='B' || c1!='M') { return FALSE; }
  59.  
  60.   bfSize = getint(fp);
  61.   getshort(fp);         /* reserved and ignored */
  62.   getshort(fp);
  63.   bfOffBits = getint(fp);
  64.  
  65.   biSize          = getint(fp);
  66.   biWidth         = getint(fp);
  67.   biHeight        = getint(fp);
  68.   biPlanes        = getshort(fp);
  69.   biBitCount      = getshort(fp);
  70.   biCompression   = getint(fp);
  71.   biSizeImage     = getint(fp);
  72.   biXPelsPerMeter = getint(fp);
  73.   biYPelsPerMeter = getint(fp);
  74.   biClrUsed       = getint(fp);
  75.   biClrImportant  = getint(fp);
  76.  
  77.   *Width = biWidth;
  78.   *Height = biHeight;
  79.   *Planes = biPlanes;
  80.   *BitsPerPixel = biBitCount;
  81.  
  82. /*
  83.   if (DEBUG>1) {
  84.     fprintf(stderr,"\nLoadBMP:\tbfSize=%d, bfOffBits=%d\n",bfSize,bfOffBits);
  85.     fprintf(stderr,"\t\tbiSize=%d, biWidth=%d, biHeight=%d, biPlanes=%d\n",
  86.         biSize, biWidth, biHeight, biPlanes);
  87.     fprintf(stderr,"\t\tbiBitCount=%d, biCompression=%d, biSizeImage=%d\n",
  88.         biBitCount, biCompression, biSizeImage);
  89.     fprintf(stderr,"\t\tbiX,YPelsPerMeter=%d,%d  biClrUsed=%d, biClrImp=%d\n",
  90.         biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant);
  91.   }
  92.  
  93.   if (ferror(fp)) { bmpError(fname,"EOF reached in file header"); goto ERROR; }
  94. */
  95.  
  96.   /* error checking */
  97. /*
  98.   if ((biBitCount!=1 && biBitCount!=4 && biBitCount!=8 && biBitCount!=24) || 
  99.       biPlanes!=1 || biCompression>BI_RLE4) {
  100.  
  101.     sprintf(buf,"Bogus BMP File!  (bitCount=%d, Planes=%d, Compression=%d)",
  102.         biBitCount, biPlanes, biCompression);
  103.  
  104.     bmpError(fname, buf);
  105.     goto ERROR;
  106.   }
  107.  
  108.   if (((biBitCount==1 || biBitCount==24) && biCompression != BI_RGB) ||
  109.       (biBitCount==4 && biCompression==BI_RLE8) ||
  110.       (biBitCount==8 && biCompression==BI_RLE4)) {
  111.  
  112.     sprintf(buf,"Bogus BMP File!  (bitCount=%d, Compression=%d)",
  113.         biBitCount, biCompression);
  114.  
  115.     bmpError(fname, buf);
  116.     goto ERROR;
  117.   }
  118.  
  119. */
  120.   
  121.   /* skip ahead to colormap, using biSize */
  122.   c = biSize - 40;    /* 40 bytes read from biSize to biClrImportant */
  123.   for (i=0; i<c; i++) getc(fp);
  124.  
  125.   /* load up colormap, if any */
  126.  
  127.   if (biBitCount!=24) {
  128.     int i, cmaplen;
  129.  
  130.     cmaplen = 1 << biBitCount;
  131.     char dummyCh;
  132.     for (i=0; i<cmaplen; i++) {
  133.       dummyCh = getc(fp);
  134.       dummyCh = getc(fp);
  135.       dummyCh = getc(fp);
  136.       getc(fp);         /* unused */
  137.     }
  138.   }
  139.   
  140.   /* load up the image */
  141. //  if      (biBitCount == 1) rv = loadBMP1(fp,pic8,biWidth,biHeight);
  142.  
  143.   return TRUE;
  144. }
  145.  
  146. static int scanLineWidth = 0;
  147.  
  148. Bool OutputBitmapHeader(FILE *fd, Bool isWinHelp = FALSE)
  149. {
  150.   int Width, Height, Planes, BitsPerPixel;
  151.   if (!GetBMPHeader(fd, &Width, &Height, &Planes, &BitsPerPixel))
  152.     return FALSE;
  153.  
  154.   scanLineWidth = (int)(Width/(8/BitsPerPixel));
  155.   int goalW = 15*Width;
  156.   int goalH = 15*Height;
  157.  
  158.   TexOutput("{\\pict");
  159.   if (isWinHelp) TexOutput("\\wbitmap");
  160.   else TexOutput("\\dibitmap");
  161.  
  162.   char buf[50];
  163.   TexOutput("\\picw"); sprintf(buf, "%d", Width); TexOutput(buf);
  164.   TexOutput("\\pich"); sprintf(buf, "%d", Height); TexOutput(buf);
  165.   TexOutput("\\wbmbitspixel"); sprintf(buf, "%d", BitsPerPixel); TexOutput(buf);
  166.   TexOutput("\\wbmplanes"); sprintf(buf, "%d", Planes); TexOutput(buf);
  167.   TexOutput("\\wbmwidthbytes"); sprintf(buf, "%d", scanLineWidth); TexOutput(buf);
  168.   TexOutput("\\picwgoal"); sprintf(buf, "%d", goalW); TexOutput(buf);
  169.   TexOutput("\\pichgoal"); sprintf(buf, "%d", goalH); TexOutput(buf);
  170.   TexOutput("\n");
  171.   return TRUE;
  172. }
  173.  
  174. Bool OutputBitmapData(FILE *fd)
  175. {
  176.   fseek(fd, 14, SEEK_SET);
  177.   int bytesSoFar = 0;
  178.   int ch = getc(fd);
  179.   char hexBuf[3];
  180.   while (ch != EOF)
  181.   {
  182.     if (bytesSoFar == scanLineWidth)
  183.     {
  184.       bytesSoFar = 0;
  185.       TexOutput("\n");
  186.     }
  187.     DecToHex(ch, hexBuf);
  188.     TexOutput(hexBuf);
  189.     bytesSoFar ++;
  190.     ch = getc(fd);
  191.   }
  192.   TexOutput("\n}\n");
  193.   return TRUE;
  194. }
  195.